<?php

class ManagerGroupsAction extends fvAction {
    
    function __construct () {
        parent::__construct(fvSite::$Layoult);
    }
    
    function executeIndex() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    
    function executeEdit() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeSave() {
        $request = fvRequest::getInstance();
        
        if (!$ManagerGroup = ManagerGroupManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $ManagerGroup = new ManagerGroup();
        }
        $ManagerGroup->updateFromRequest($request->getRequestParameter('mg'));
        
        if ($ManagerGroup->save()) {
            $this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
            fvResponce::getInstance()->setHeader('Id', $ManagerGroup->getPk()); 
            $Managers = ManagerManager::getInstance()->getAll('group_id = ? AND inherit = 1', null, null, $ManagerGroup->getPk());
            
            foreach ($Managers as $Manager) {
                $Manager->permitions = $ManagerGroup->permitions;
                $Manager->save();
            }
        } else { 
            fvResponce::getInstance()->setHeader('X-JSON', json_encode($ManagerGroup->getValidationResult()));
            $this->setFlash("Ошибка при сохранении данных проверте правильность введенных данных", self::$FLASH_ERROR);
        }
        if ($request->getRequestParameter('redirect')) {
            fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        }        
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeDelete() {
        $request = fvRequest::getInstance();
        if (!$ManagerGroup = ManagerGroupManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
        } else {
            $ManagerGroup->delete();
            $this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
        }
        
        fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
    
    function executeGetparams() {
        if (!fvRequest::getInstance()->isXmlHttpRequest()) return false;

        $Group = ManagerGroupManager::getInstance()->getByPk(fvRequest::getInstance()->getRequestParameter("group_id"));
        
        if (!($Group instanceof ManagerGroup)) return false;
        
        fvResponce::getInstance()->setHeader('X-JSON', json_encode($Group->permitions));
        return self::$FV_AJAX_CALL;
    }
}

?>
